package org.dtrd.modules.drug.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.drug.entity.bean.DrugDetail;
import org.dtrd.modules.drug.entity.bean.DrugListInfo;
import org.dtrd.modules.drug.entity.po.DtrdExtEmsYwDrugs;
import org.dtrd.modules.drug.entity.request.DrugPageRequest;
import org.dtrd.modules.drug.mapper.DtrdExtEmsYwDrugsMapper;
import org.dtrd.modules.drug.service.IDtrdExtEmsYwDrugCategoryService;
import org.dtrd.modules.drug.service.IDtrdExtEmsYwDrugsService;
import org.dtrd.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 药品库 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-02
 */
@Service
@DS("multi-datasource1")
public class DtrdExtEmsYwDrugsServiceImpl extends ServiceImpl<DtrdExtEmsYwDrugsMapper, DtrdExtEmsYwDrugs> implements IDtrdExtEmsYwDrugsService {

    @Autowired
    private IDtrdExtEmsYwDrugCategoryService drugCategoryService;

    @Override
    public List<DrugDetail> getDrugsList(Integer requestId) {
        LambdaQueryWrapper<DtrdExtEmsYwDrugs> queryWrapper = baseQueryWrapper();
        List<DtrdExtEmsYwDrugs> poList = list(queryWrapper);
        List<DrugDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new DrugDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public DrugDetail getDrugs(Integer dataId) {
        DtrdExtEmsYwDrugs dtrdExtEmsYwDrugs = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwDrugs)
                        .map(po -> new DrugDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateDrug(DrugDetail dto) {
        DtrdExtEmsYwDrugs dtrdExtEmsYwDrugs = new DtrdExtEmsYwDrugs();
        if(dto.getDataId() == null){
            dtrdExtEmsYwDrugs = dtrdExtEmsYwDrugs.init();
        }
        dtrdExtEmsYwDrugs.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwDrugs);
    }

    @Override
    public boolean removeDrugs(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public IPage<DrugListInfo> getDrugPage(DrugPageRequest request) {
        // 查询 poPageList
        Integer pageNum = request.getPage();
        Integer pageSize = request.getPageSize();

        List<Integer> drugCategoryIds = Optional.ofNullable(request.getCategoryId()).map(id -> drugCategoryService.getDrugCategoryIds(id)).orElse(null);

        LambdaQueryWrapper<DtrdExtEmsYwDrugs> queryWrapper = baseQueryWrapper()
                .in(CollectionUtil.isNotEmpty(drugCategoryIds),DtrdExtEmsYwDrugs::getDrugCategoryid, drugCategoryIds);
        // 拼接模糊查询条件
        if (StrUtil.isNotBlank(request.getDrugName())) {
            queryWrapper.like(DtrdExtEmsYwDrugs::getDrugName, StringUtil.fuzzySearchWrapper(request.getDrugName()));
        }
        if (StrUtil.isNotBlank(request.getDrugIndication())) {
            queryWrapper.like(DtrdExtEmsYwDrugs::getDrugIndication, StringUtil.fuzzySearchWrapper(request.getDrugIndication()));
        }
        queryWrapper.orderByDesc(DtrdExtEmsYwDrugs::getDrugCategoryid);
        IPage<DtrdExtEmsYwDrugs> requestPage = new Page<>(pageNum, pageSize);
        IPage<DtrdExtEmsYwDrugs> queryPage = this.page(requestPage, queryWrapper);
        // 转换类型即设置分类名称
        Map<Integer, String> categoryIdToName = drugCategoryService.getDrugCategoryIdToName();
        IPage<DrugListInfo> drugListInfoIPage = new Page<>(pageNum, pageSize);
        if (CollectionUtil.isNotEmpty(requestPage.getRecords())) {
            drugListInfoIPage = queryPage.convert(po -> {
                DrugListInfo drugListInfo = new DrugListInfo().parseFromPo(po);
                Integer drugCategoryid = po.getDrugCategoryid();
                drugListInfo.setDrugCategoryName(categoryIdToName.get(drugCategoryid));
                return drugListInfo;
            });
        }
        return drugListInfoIPage;
    }

    private LambdaQueryWrapper<DtrdExtEmsYwDrugs> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwDrugs.class)
                        .eq(DtrdExtEmsYwDrugs::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwDrugs> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwDrugs.class)
                        .eq(DtrdExtEmsYwDrugs::getIsDel, 0);
    }
}
